package cm.example.shoppingplatformspringboot.mapper;

import cm.example.shoppingplatformspringboot.entity.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressMapper {
    // 新增：根据用户ID查询地址
    @Select("SELECT * FROM wn_address WHERE user_id = #{userId}")
    List<Address> findByUserId(Long userId);

    // 新增：插入地址
    @Insert("INSERT INTO wn_address (user_id, accept, telphone, province, city, area, street, def) " +
            "VALUES (#{user_id}, #{accept}, #{telphone}, #{province}, #{city}, #{area}, #{street}, #{def})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertAddress(Address address);

    // 新增：更新地址
    @Update("UPDATE wn_address SET accept=#{accept}, telphone=#{telphone}, province=#{province}, " +
            "city=#{city}, area=#{area}, street=#{street}, def=#{def} WHERE id=#{id}")
    int updateAddress(Address address);

    // 新增：删除地址
    @Delete("DELETE FROM wn_address WHERE id = #{id}")
    int deleteAddress(Long id);

    // 新增：取消用户的其他默认地址（修改地址的时候，取消其他默认地址）
    @Update("UPDATE wn_address SET def='N' WHERE user_id=#{userId} AND id!=#{addressId}")
    void cancelOtherDefaultAddresses(Long userId, Long addressId);

    // 新增：取消用户的其他默认地址（前端添加地址的时候，自己设置默认地址）
    @Update("UPDATE wn_address SET def='N' WHERE user_id=#{userId}")
    void setDefaultAddress(Long userId);
}